package com.qingcheng.task;

import com.qingcheng.dao.SeckillGoodsMapper;
import com.qingcheng.entity.SeckillStatus;
import com.qingcheng.pojo.seckill.SeckillGoods;
import com.qingcheng.pojo.seckill.SeckillOrder;
import com.qingcheng.util.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * Welcome to the most Amazing IntelliJ IIIDEA.
 * Creat0r: ^_^ fffantianxing ^_^
 * Date: 2019-07-25
 * Time: 20:57
 */
@Component
public class MultiThreadingCreateOrder {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private SeckillGoodsMapper seckillGoodsMapper;

    @Async
    public void createOrder() {


        try {
            SeckillStatus seckillStatus = (SeckillStatus) redisTemplate.boundListOps("SeckillOrderQueue").rightPop();

            if (seckillStatus!=null){
                //时间区间            
                String time = seckillStatus.getTime();
                //用户登录名
                String username = seckillStatus.getUsername();
                //用户抢购商品
                String id = seckillStatus.getGoodsId();

                System.out.println("准备开始执行。。");
                Thread.sleep(10000);


                SeckillGoods seckillGoods = (SeckillGoods) redisTemplate.boundHashOps("seckillGoods" + time).get(id);
                //如果没有库存，直接抛异常
                if (seckillGoods == null) {
                    throw new RuntimeException("商品已售罄");
                }

                //如果有库存，直接生成订单
                SeckillOrder seckillOrder = new SeckillOrder();
                seckillOrder.setId(idWorker.nextId() + "");
                seckillOrder.setCreateTime(new Date());
                seckillOrder.setMoney(seckillGoods.getCostPrice());
                seckillOrder.setSeckillId(id);
                seckillOrder.setUserId(username);
                seckillOrder.setSellerId(seckillGoods.getSellerId());
                seckillOrder.setStatus("0");

                //将秒杀订单添加到redis
                redisTemplate.boundHashOps("SeckillOrder").put(username, seckillOrder);

                seckillStatus.setStatus(2);
                seckillStatus.setMoney(seckillOrder.getMoney());
                seckillStatus.setOrderId(seckillOrder.getId());
                //抢单成功，更新抢单状态，排队->等待支付
                redisTemplate.boundHashOps("UserQueueStatus").put(username,seckillStatus);

                //更新库存
                seckillGoods.setStockCount(seckillGoods.getStockCount() - 1);

                if (seckillGoods.getStockCount() <= 0) {
                    seckillGoodsMapper.updateByPrimaryKeySelective(seckillGoods);
                    //删除redis中的商品信息
                    redisTemplate.boundHashOps("seckillGoods" + time).delete(id);
                } else {//库存不为0，将库存数量更新到缓存

                    redisTemplate.boundHashOps("seckillGoods" + time).put(id, seckillGoods);

                }

            }

        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
